let handle: any = null

async function sleep(time: number) {
  return new Promise((r) => {
    setTimeout(() => {r(0)}, time)
  })
}

async function getExternalJs () {
  if (handle) {
    return handle
  }
  console.log("get handle...........")
  await sleep(5000)
  handle = {
    write (msg: string) {
      console.log(msg)
    }
  }

  return handle
}

async function job1() {
  let handle: any = await getExternalJs()
  handle.write(1111111111)
}

async function job2() {
  let handle: any = await getExternalJs()
  handle.write(2222222222)
}

async function main () {
  await [job1(), job2()]
}

main().catch((err) => {console.error(err)})